import re

BFH_TAG = "xmtj_bfh"


def dealSina(s):
    s = str(s).replace("%", BFH_TAG).replace(" ", "").replace("\\", "")
    return dealMPVideo(dealTopic(dealNetUrl(dealMoji(showAll(atSomeOne(dealWBStore(dealImg(s)))))))).replace(BFH_TAG,
                                                                                                             "%")


# <imgsrc='//img.t.sinajs.cn/t4/appstyle/expression/emimage/ee858c.png'width="20px"height="20px">
def dealImg(s):
    print(s)
    imagePattern = re.compile(u"<imgsrc='(.*?)'width=.*?height=.*?>", re.S)
    l = re.findall(imagePattern, s)
    print(l)
    replacePattern = re.compile(u"<imgsrc='.*?'width=.*?height=.*?>", re.S)
    str = re.sub(replacePattern, "%s", s)
    imgTag = "<img src=\"http:{}\" alt=\"Smiley face\" style=\"float:left\"> "

    li = []
    for i in l:
        li.append(imgTag.format(i))
    if len(li) == 0:
        return s
    else:
        return str % tuple(li)


# 为可爱的绒绒打call！拍的蛛魇真的太太太好看啦！<imgclass="W_img_face"render="ext"src="//img.t.sinajs.cn/t4/appstyle/expression/ext/normal/40/pcmoren_tian_org.png"title="[舔屏]"alt="[舔屏]"type="face"/><imgclass="W_img_face"render="ext"src="//img.t.sinajs.cn/t4/appstyle/expression/ext/normal/40/pcmoren_tian_org.png"title="[舔屏]"alt="[舔屏]"type="face"/><imgclass="W_img_face"render="ext"src="//img.t.sinajs.cn/t4/appstyle/expression/ext/normal/40/pcmoren_tian_org.png"title="[舔屏]"alt="[舔屏]"type="face"/>店主决定转发此微博并关注官博转发过一百抽两位姑娘送蜘蛛戒指啦！！！为姑娘们谋福利啊！！蛛魇定金过几天就截止啦！！店铺号：13243125QQ群：575832874加入QQ群可以解锁更多大图哦！
def dealMoji(s):
    imagePattern = re.compile(u'<imgclass="W_img_face"render="ext"src="(.*?)"title=".*?"alt=".*?"type="face"/>', re.S)
    l = re.findall(imagePattern, s)
    replacePattern = re.compile(u'<imgclass="W_img_face"render="ext"src=".*?"title=".*?"alt=".*?"type="face"/>', re.S)
    str = re.sub(replacePattern, "%s", s)
    imgTag = "<img src=\"http:{}\" alt=\"Smiley face\" style=\"float:left\"> "

    li = []
    for i in l:
        li.append(imgTag.format(i))
    if len(li) == 0:
        return s
    else:
        return str % tuple(li)


# <atarget="_blank"href="//weibo.com/5901077939/FuqlsDMn3"class="WB_text_opt"suda-uatrack="key=original_blog_unfold&value=click_unfold:4172511149480733:5901077939"action-type="fl_unfold"action-data="mid=4172511149480733&is_settop&is_sethot&is_setfanstop&is_setyoudao">展开全文<iclass="W_ficonficon_arrow_down">c</i></a>
def showAll(s):
    hrefPattern = re.compile(
        u'<atarget="_blank"href="(.*?)"class="WB_text_opt"suda-uatrack=".*?">展开全文<iclass="W_ficonficon_arrow_down">c</i></a>',
        re.S)
    l = re.findall(hrefPattern, s)
    replacePattern = re.compile(
        u'<atarget="_blank"href=".*?"class="WB_text_opt"suda-uatrack=".*?">展开全文<iclass="W_ficonficon_arrow_down">c</i></a>',
        re.S)
    str = re.sub(replacePattern, "%s", s)

    hrefTag = "<a href=\"http:{}\">展开全文</a>"
    lh = []
    for h in l:
        lh.append(hrefTag.format(h))
    if len(lh) == 0:
        return s
    else:
        return str % tuple(lh)


# <a target="_blank" render="ext" extra-data="type=atname" href="//weibo.com/n/Lo%E5%A8%98%E8%84%91%E6%B4%9E%E5%B0%8F%E5%A4%A9%E5%9D%91_lolita?from=feed&loc=at" usercard="name=Lo娘脑洞小天坑_lolita">@Lo娘脑洞小天坑_lolita</a>
def atSomeOne(s):
    hrefPattern = re.compile(
        u'<atarget="_blank"render="ext"extra-data="type=atname"href="(.*?)"usercard="name=.*?">(.*?)</a>', re.S)
    l = re.findall(hrefPattern, s)
    replacePattern = re.compile(
        u'<atarget="_blank"render="ext"extra-data="type=atname"href=".*?"usercard="name=.*?">.*?</a>',
        re.S)
    str = re.sub(replacePattern, "", s)
    return str
    # 可以点击的跳转
    # str = re.sub(replacePattern, "%s", s)
    # atTag = "<a href=\"http:%s\">%s</a>"
    # la = []
    # for t in l:
    #     la.append(atTag % t)
    # if len(la) == 0:
    #     return s
    # else:
    #     return str % tuple(la)


# <a suda-uatrack="key=minicard&value=pagelink_minicard_click" title="网页链接" href="http://t.cn/RWes8bD" alt="http://t.cn/RWes8bD" action-type="feed_list_url" target="_blank" ><i class="W_ficon ficon_cd_link">O</i>网页链接</a>
def dealNetUrl(s):
    hrefPattern = re.compile(
        u'<asuda-uatrack="key=minicard&value=pagelink_minicard_click"title=".*?"href="(.*?)"alt=".*?"action-type="feed_list_url"target="_blank"><.*?class="W_ficon ficon_cd_link">O</i>.*?</a>',
        re.S)
    l = re.findall(hrefPattern, s)

    replacePattern = re.compile(
        u'<asuda-uatrack="key=minicard&value=pagelink_minicard_click"title=".*?"href=".*?"alt=".*?"action-type="feed_list_url"target="_blank"><.*?class="W_ficon ficon_cd_link">O</i>.*?</a>',
        re.S)
    str = re.sub(replacePattern, "", s)
    return str
    # 可以点击的跳转
    # str = re.sub(replacePattern, "%s", s)
    # netTag = "<a href=\"%s\">网页链接</a>"
    # la = []
    # for t in l:
    #     la.append(netTag % t)
    #
    # if len(la) == 0:
    #     return s
    # else:
    #     return str % tuple(la)


# <a target="_blank" render="ext" suda-uatrack="key=topic_click&value=click_topic" class="a_topic" extra-data="type=topic" href="//huati.weibo.com/k/%E5%A4%A9%E9%B9%85%E4%B9%8B%E5%BF%83?from=501">#天鹅之心#</a>
def dealTopic(s):
    hrefPattern = re.compile(
        u'<atarget="_blank"render="ext"suda-uatrack="key=topic_click&value=click_topic"class="a_topic"extra-data="type=topic"href="(.*?)">(.*?)</a>',
        re.S)
    l = re.findall(hrefPattern, s)
    replacePattern = re.compile(
        u'<atarget="_blank"render="ext"suda-uatrack="key=topic_click&value=click_topic"class="a_topic"extra-data="type=topic"href=".*?">.*?</a>',
        re.S)
    str = re.sub(replacePattern, "", s)
    return str
    # str = re.sub(replacePattern, "%s", s)
    # topicTag = "<a href=\"http:%s\">%s</a>"
    # la = []
    # for t in l:
    #     la.append(topicTag % t)
    # if len(la) == 0:
    #     return s
    # else:
    #     return str % tuple(la)


# <a class="video_link" href="http://t.cn/RYlr5LO" target="_blank" suda-data="key=tblog_card&amp;value=click_title::2017607_video:2017607:8e66d8c48f78d858848765ad3bfbd407:search_ajax:::">AthenaApol...</a>
def dealMPVideo(s):
    hrefPattern = re.compile(
        u'<.*?class="video_link"href="(.*?)"target="_blank"suda-data="key=tblog_card&amp;value=click_title:.*?:search_ajax:::">(.*?)</a>',
        re.S)
    l = re.findall(hrefPattern, s)

    replacePattern = re.compile(
        u'<.*?class="video_link"href=".*?"target="_blank"suda-data="key=tblog_card&amp;value=click_title:.*?:search_ajax:::">.*?</a>',
        re.S)
    str = re.sub(replacePattern, "", s)
    return str
    # str = re.sub(replacePattern, "%s", s)
    # netTag = '<a href="%s">VIDEO:"%s"</a>'
    # la = []
    # for t in l:
    #     la.append(netTag % t)
    # if len(la) == 0:
    #     return s
    # else:
    #     return str % tuple(la)


def dealWBStore(s):
    hrefPattern = re.compile(
        u'<asuda-uatrack="key=tblog_card&value=click_title.*?"title=".*?"href="(.*?)"alt=".*?"action-type="feed_list_url"target="_blank"><iclass="W_ficonficon_cd_link">O</i>(.*?)</a>',
        re.S)
    l = re.findall(hrefPattern, s)
    replacePattern = re.compile(
        u'<asuda-uatrack="key=tblog_card&value=click_title.*?"title=".*?"href=".*?"alt=".*?"action-type="feed_list_url"target="_blank"><iclass="W_ficonficon_cd_link">O</i>.*?</a>',
        re.S)
    str = re.sub(replacePattern, "", s)
    return str
    # str = re.sub(replacePattern, "%s", s)
    # hrefTag = "<a href=\"%s\">%s</a>"
    # lh = []
    # for h in l:
    #     lh.append(hrefTag % h)
    # if len(lh) == 0:
    #     return s
    # else:
    #     return str % tuple(lh)
